<?php 
session_start();
require 'authentication.php';
include 'db_con.php';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Tractors</title>
		<link rel="stylesheet" type="text/css" href="style/style.css">
		<script src="formWrapper.js"></script> 
		<script type="text/javascript">
			function getparams()
			{
				var str = "";
				var checkboxes = document.getElementsByTagName("input");
				var len = checkboxes.length;
				var elem = new Array();
				
			    for (var i = 0; i < len; i++) 
			    {
			        if (checkboxes[i].type === "checkbox") 
			        {
			            if(checkboxes[i].checked)
			            {
			            	elem[i] = checkboxes[i].name;
			            	str += checkboxes[i].name+",";
			            }
			        }
			    }        
			    str = str.substr(0,str.length - 1);
				var xmlhttp;
				if (str=="")
				  {
				  document.getElementById("txtHint").innerHTML="";
				  return;
				  }
				if (window.XMLHttpRequest)
				  {
				  xmlhttp=new XMLHttpRequest();
				  }
				else
				  {
				  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				  }
				xmlhttp.onreadystatechange=function()
				  {
				  if (xmlhttp.readyState==4 && xmlhttp.status==200)
				  {
					    if(xmlhttp.responseText == "-1")
					    {
					    	document.getElementById("txtHint").innerHTML="not enough money";
					    }
					    else if(xmlhttp.responseText == "-2")
					    {
					    	document.getElementById("txtHint").innerHTML="please try again";
					    }
					    else if(xmlhttp.responseText == "-2")
					    {
					    	document.getElementById("txtHint").innerHTML="please try again22";
					    }
					    else
					    {
					    	//document.getElementById("txtHint").innerHTML= xmlhttp.responseText;
					    	location.href = "index.php?p=1";
					    }
				   }
				  }
				xmlhttp.open("GET","buyTractor.php?q="+str,true);
				xmlhttp.send();
				
			}			
		</script>
	</head>
	<body>';
		echo '<div class="wrap">
			<div class="inner_wrap">
				<div class="header"></div>
				<div class="left_menu">
                    <p><a href="formAddTractor.php">ADD</a></p>
    				<p><a href="#" id="formDeleteTractor" onclick="manageForm(\'formDeleteTractor\');"> DELETE </a></p>
    				<p><a href="#" id="formEditTractor" onclick="manageForm(\'formEditTractor\');"> EDIT </a></p>
                    <p><a href="#" onclick="getparams();">BUY</a></p>
				</div>
				<div class="content">
					<div class="wrap_table">
						<form method="post" id="contentForm">
						<table class="table">';
							echo '<tr><th>Picture</th><th>Name</th><th>Price</th><th>Check</th></tr>';
							$query = "SELECT * FROM `tractor`";
							$result = mysqli_query($db_con, $query);
							if(mysqli_num_rows($result) != 0){
								while($row = mysqli_fetch_assoc($result)){
									echo '<tr>
                                        <td><img src="pic/'.$row['pic_path'].'" width="75" height="50" /></td>
                                        <td><p>'.$row['name'].'</p></td>
                                        <td><p>'.$row['price'].'</p></td>
                                        <td><input type="checkbox" name="'. $row['name'] . '" value="' . $row['tractor_id'] .'"/></td>
                                    </tr>';
								}
							}
						echo '</table>
					</form>
					</div>
						<div id="txtHint">'; 
							if(isset($_GET['p']))
							{
								if($_GET['p'] == 1)
								{
									echo "Success!";
								}
							}
				echo '</div>					
				</div>					
			</div>		
		</div>';
	echo '</body>
</html>';
	
?>